package Thread;
// 线程的创建（2）

/**
 * 实现Runnable接口并重写run方法
 */
class MyRunnable implements Runnable{
    @Override
    public void run() {
        while(true) {
            System.out.println("hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
public class demo2 {
    public static void main(String[] args) throws InterruptedException {
        // MyRunnable类实现了Runnable接口，MyRunnable的实例可以被赋值给Runnable类型的引用变量runnable。向上转型
        Runnable runnable = new MyRunnable();
        Thread t = new Thread(runnable);
        t.start();

        while(true) {
            System.out.println("hello main");
            Thread.sleep(1000);
        }
    }
}
